package org.example.ski_demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.ski_demo.entity.Equipment;
import org.example.ski_demo.service.EquipmentService;
import org.example.ski_demo.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2025-07-04
 */
@RestController
@RequestMapping("/equipment")
public class EquipmentController {
    @Autowired
    private EquipmentService equipmentService;

    @GetMapping("/list")
    public List<Equipment> list() {
        return this.equipmentService.list();
    }

    @PostMapping("/rent/{eid}/{uid}")
    public Boolean rent(@PathVariable("eid") Integer eid, @PathVariable("uid") Integer uid){
        Equipment equipment = this.equipmentService.getById(eid);
        equipment.setStatus("租出");
        equipment.setUid(uid);
        return this.equipmentService.updateById(equipment);
    }

    @GetMapping("/load")
    public PageVO load(
            @RequestParam("page") Integer page,
            @RequestParam("size") Integer size,
            @RequestParam(value = "keyWord",required = false) String keyWord,
            @RequestParam(value = "type",required = false) String type
    ){
        Page<Equipment> pageModel = new Page<>(page, size);
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(keyWord),type,keyWord);
        Page<Equipment> resultPage = this.equipmentService.page(pageModel, queryWrapper);
        return new PageVO(resultPage.getRecords(),resultPage.getSize(),resultPage.getTotal());
    }

    @PostMapping("/add")
    public Boolean add(@RequestBody Equipment equipment){
        return this.equipmentService.save(equipment);
    }

    @PutMapping("/update")
    public Boolean update(@RequestBody Equipment equipment){
        return this.equipmentService.updateById(equipment);
    }

    @DeleteMapping("/delete/{id}")
    public Boolean delete(@PathVariable("id") Integer id){
        return this.equipmentService.removeById(id);
    }

}

